#!/usr/bin/env python
"""
# -*- coding: utf-8 -*-
# @Time    : 2020/9/17 22:08
# @Author  : AshJo
# @Desc    : sys.argv练习
# @Filename: sys_argv.py
# @Version : v1.0
"""

import os
import sys
import shutil


# filename = sys.argv[0]
# username = sys.argv[1]
# password = sys.argv[2]
# command = sys.argv[3]
# src_path = sys.argv[4]
# des_path = sys.argv[5]
# print(sys.argv)
# ['sys_argv.py', 'alex', 'sb', 'cp', 'D:\\VScode\\Python\\Project\\Python_fullstack\\day22\\github_icon.html', 'D:\\
# VScode\\Python\\Project\\Python_fullstack\\day22\\github_icon.html.bak']
# print(argv0, argv1, argv2, argv3, argv4, argv5)


def copy_func(src_path, dst_path):
    filename = os.path.basename(src_path)
    path = os.path.join(dst_path, filename)
    shutil.copy2(src_path, path)


def rm_func(path):
    if os.path.isfile(path):
        os.remove(path)
    else:
        shutil.rmtree(path)


def rename_func(src_name, dst_name):
    if os.path.isfile(src_name):
        os.rename(src_name, dst_name)
    else:
        shutil.move(src_name, dst_name, copy_function=shutil.copy2())


username = 'alex'
password = 'sb'
if __name__ == '__main__':
    if len(sys.argv) >= 5:
        if sys.argv[1] == username and sys.argv[2] == password:
            if sys.argv[3] == 'cp' and len(sys.argv) == 6:
                if os.path.exists(sys.argv[4]) and os.path.exists(sys.argv[5]):
                    copy_func(sys.argv[4], sys.argv[5])
            elif sys.argv[3] == 'rm' and len(sys.argv) == 5:
                if os.path.exists(sys.argv[4]):
                    rm_func(sys.argv[4])
            elif sys.argv[3] == 'rename' and len(sys.argv) == 6:
                if os.path.exists(sys.argv[4]):
                    rename_func(sys.argv[4], sys.argv[5])
            else:
                print('input is Error!')
        else:
            print('username or password is not True')

    else:
        print('输入的命令长度有误！')
